﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

using SBPweb.RIA.Shared.Contracts.Extensibility;
using SBPweb.RIA.Client.Controls.Extensions;

namespace SBPweb.RIA.Client.Controls.Base
{

    public class UserControlBase : UserControl, INotifyPropertyChanged
    {

        #region Private fields

        private object oId;

        #endregion

        #region Constructor logic

        protected UserControlBase()
        {
            //this.ObjectId = PersistencytExtentions.ObjectId++;
            this.Loaded += new RoutedEventHandler(OnLoaded);
            this.Unloaded += new RoutedEventHandler(OnUnloaded);
        }
        
        //~ UserControlBase()
        //{
        //    Dispatcher.BeginInvoke(() =>
        //    {
        //        FinalizeHandlers();
        //    });
        //}

        #endregion

        #region Property accessors

        internal object ObjectId
        {
            get
            {
                return this.GetObjectId(ref oId, Name);
            }
        }

        public ExtendableApplication Application
        {
            get
            {
                return System.Windows.Application.Current as ExtendableApplication;
            }
        }

        public IExtendable ApplicationInitialParameters
        {
            get
            {
                return Application.Get<IExtendable>("InitialParameters");
            }
        }

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        protected virtual void InitializeHandlers()
        {
        }

        protected virtual void FinalizeHandlers()
        {
        }
        
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        protected T GetValue<T>(DependencyProperty property)
        {
            return (T)base.GetValue(property);
        }

        protected void SetValue<T>(DependencyProperty property, T value)
        {
            SetValue<T>(property, value, null);
        }

        protected void SetValue<T>(DependencyProperty property, T value, string propertyName)
        {
            base.SetValue(property, value);
            if (!string.IsNullOrEmpty(propertyName))
            {
                RaisePropertyChanged(propertyName);
            }
        }

        #endregion

        #region Event handlers

        protected virtual void OnLoaded(object sender, RoutedEventArgs e)
        {
            InitializeHandlers();
        }

        protected virtual void OnUnloaded(object sender, RoutedEventArgs e)
        {
            FinalizeHandlers();
        }

        #endregion

    }

}